import Layout from '@/views/Layout'
import Login from '@/views/Login'
import Register from '@/views/Register'
import Vue from 'vue'
import VueRouter from 'vue-router'

import { getToken } from '@/api/storage'
import { Message } from 'element-ui'
import StudentVue from '@/views/Student.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/login', component: Login },
  { path: '/register', component: Register },
  {
    path: '/',
    component: Layout,
    redirect: 'student',
    children: [
      { path: 'student', component: StudentVue }
    ]
  }
]

const router = new VueRouter({
  routes
})

const whiteList = ['/login', '/register']
router.beforeEach((to, from, next) => {
  if (whiteList.includes(to.path)) {
    next()
    return
  }
  if (getToken()) {
    next()
    return
  }
  next('/login')
  Message.error('身份过期，请重新登录')
})

export default router
